import logging 
import os 
import time 
import random
import traceback
import time
#import serial
import sys
def get_time_str():
	ltime = time.localtime(time.time())
	timeStr = time.strftime("%Y-%m-%d %H:%M:%S", ltime)
	return timeStr

def file_write(data_str, path, make_path=None):
	if not isinstance(data_str, (str)):
		raise TypeError('bad operand type, parameter must be a string')
	timeStr = get_time_str()
	#print(timeStr)
	if make_path != None:
		if os.path.exists("./%s/%s" %(path,make_path)) == False:
			os.makedirs("./%s/%s/" %(path, make_path))
		fileStr = timeStr.split(' ')[1]
		fileStr = fileStr.split(':')[0]+'.txt'	
		data_path = "./%s/%s/%s" %(path,make_path,fileStr)
	else:
		fileStr = timeStr.split(' ')[1]
		fileStr = fileStr.split(':')[0]+'.txt'	
		data_path = "./%s/%s" %(path,fileStr) 
	fp = open(data_path, 'a')
	fp.write(data_str+'*'+str(timeStr)+'\n')
	fp.close()
	return True


def data_write(data):
	data_str = str(data)
	TimeStr = get_time_str();
	make_path = TimeStr.split(' ')[0]	
	#print(make_path)
	file_write(data_str,'raw_data',make_path)

def main():
	data_write([{"id":5,"power":671,"state":1}])
	data_write([{"id":6,"power":671,"state":1}])
	#print(type([{"id":5,"power":671,"state":1}]))

if __name__ == '__main__':
	main()
